package main

import (
	"fmt"
	"sync"
	"sync/atomic"
)

var mu sync.Mutex

func AutomaticAdd(num int) int32 {
	wg := sync.WaitGroup{}
	var ret int32
	ret = 0
	for i := 0; i < num; i++ {
		// 要先在外层+1
		wg.Add(1)
		go func(in *int32, wg *sync.WaitGroup) {
			defer wg.Done()
			// 累加操作
			//*in++
			atomic.AddInt32(in, 1)
		}(&ret, &wg)
	}
	wg.Wait()
	return ret
}

func main() {
	r1 := AutomaticAdd(100)
	fmt.Println("100 -> ", r1)
	fmt.Println("1000 -> ", AutomaticAdd(1000))
}
